package com.pzhu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pzhu.entity.Business;
import com.pzhu.entity.Code;
import com.pzhu.entity.Online;
import com.pzhu.entity.Result;
import com.pzhu.service.BusinessService;
import com.pzhu.service.OnlineService;
import com.pzhu.until.JwtTokenUntil;
import com.pzhu.until.TokenClaim;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.Date;

@RestController
@Slf4j
@RequestMapping("/api/business")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @Autowired
    private OnlineService onlineService;

    /**
     * 商家登录
     * @param session
     * @param business
     * @return
     */
    @PostMapping("/login")
    public Result login(HttpSession session, @RequestBody Business business) {


        String code = (String) session.getAttribute("code");
        LambdaQueryWrapper<Business> businessLambdaQueryWrapper = new LambdaQueryWrapper<>();
        businessLambdaQueryWrapper.eq(Business::getBusinessName, business.getBusinessName());

        Business login = businessService.getOne(businessLambdaQueryWrapper);
        log.info("登录之后的对象是:" + login);
        if (code.equalsIgnoreCase(business.getBcode())) {
            if (login != null) {
                if (login.getBusinessDel().toString().equals("1")) {
                    return new Result(Code.error, "账号已删除");
                } else {

                    if (business.getBusinessPassword().equals(login.getBusinessPassword())) {
//                        session.setAttribute("loginBusiness", login.getBusinessId());
                        String token = JwtTokenUntil.generate(new TokenClaim("loginBusiness", login.getBusinessId()));
                        UpdateWrapper<Online> onlineUpdateWrapper = new UpdateWrapper<>();
                        onlineUpdateWrapper.setSql("logincount = logincount + 1");
                        onlineUpdateWrapper.between("date", LocalDate.now().atStartOfDay(), LocalDate.now().atTime(23, 59, 59));
                        boolean update = onlineService.update(onlineUpdateWrapper);
                        if (!update) {
                            Online online = new Online(new Date(System.currentTimeMillis()), 0, 1);
                            onlineService.save(online);
                        }
                        return new Result(Code.success, "登录成功", token);
                    } else {
                        return new Result(Code.error, "密码错误");
                    }

                }
            } else {
                return new Result(Code.error, "账号不存在");
            }
        } else {
            return new Result(Code.error, "验证码错误");

        }
    }

    /**
     * 商家注册
     * @param business
     * @return
     */
    @PostMapping("/register")
    public Result regidter(@RequestBody Business business) {

        if (business != null) {

            LambdaQueryWrapper<Business> businessLambdaQueryWrapper = new LambdaQueryWrapper<>();
            businessLambdaQueryWrapper.eq(Business::getBusinessName, business.getBusinessName());

            Business one = businessService.getOne(businessLambdaQueryWrapper);
            if (one == null) {


                boolean save = businessService.save(business);
                return new Result(Code.success, "注册成功", save);
            }
            return new Result(Code.error, "账号已存在");
        }


        return null;


    }
}
